package jmind.core.support;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;

/**
 * description:  apache 根据Velocity  获取html内容
 *
 * @author weibo.xie
 * @date : create in 2:48 下午 2021/7/20
 */
public class HtmlVelocityTemplate {
    private final VelocityEngine velocityEngine;
    public HtmlVelocityTemplate(String path){
        //初始化并取得Velocity引擎
        velocityEngine = new VelocityEngine();
        Properties properties = new Properties();
        //    String basePath = "/Users/weibo/workspace/jmind/jmind-core/src/test/java/jmind/core/pdf";//这里需要这样写路径！！！
        // 设置模板的路径，绝对路径
        properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
        // 初始花velocity 让设置的路径生效
        velocityEngine.init(properties);
    }

    public  String getByTemplate(String vmTemplate, Map params)  {
        //取得velocity的模版
        Template t = velocityEngine.getTemplate(vmTemplate,"UTF-8");
        //取得velocity的上下文context
        VelocityContext context = new VelocityContext(params);
        StringWriter writer = new StringWriter();
        //把数据填入上下文
        t.merge(context, writer);
        return writer.toString();
    }
}